package com.houdoudou.com.owner;

import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import www.houdoudou.com.common.CommonFunction;

public class OwnerManagement {

 public static	JSONObject  managerOwner(String jsonData){
		JSONObject result = new JSONObject();
		JSONObject jsonfrom= JSONObject.fromObject(jsonData);				
		//判断请求的功能
		int FunctionCode=jsonfrom.getInt("FunctionCode");
		   PreparedStatement pstm=null; 
		   Statement  stm=null;
		   ResultSet rs=null; 		 
		   Connection conn= CommonFunction. getConnection();
		   // 此值代表用户身份，不同的身份，对于同一个增删改查函数，有不同的处理，防止出错。
		   int Manager_Authority= jsonfrom.getInt("Manager_Authority");
		switch(FunctionCode){
		// 查询 
		case 1:{
			
				
				// step2 查询字管理员
				  
				   String Owner_Telnum=jsonfrom.getString("Owner_Telnum");
				   try {
					   String sql = "select * from Tb_Owner where Owner_Telnum="+Owner_Telnum+"";  
						stm=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
						rs= stm.executeQuery(sql);
						if(rs.next()){

								result.put("ActionStatus", true);
								result.put("ErrorInfo", "查询成功");						
								result.put("Owner_Name", rs.getString("Owner_Name"));
								result.put("Owner_Address", rs.getString("Owner_Address"));
								result.put("Parent_Manager_Telnum", rs.getString("Parent_Manager_Telnum"));
								double temp=(double)rs.getDouble("Balance_Rate");
								result.put("Balance_Rate", temp);
								result.put("Ownner_isAuthority", rs.getString("Ownner_isAuthority"));							
								result.put("Balance_Type", rs.getString("Balance_Type"));
							
								
							
						}else{
							result.put("ActionStatus", false);
							result.put("ErrorInfo", "要查询的用户不存在");
						}
						
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						result.put("ActionStatus", false);
						result.put("ErrorInfo", "数据库查询异常");
					}    			
				   break;     } //case 1
		//修改
		case 2:{
			
			// step2 数据库输入。
			 String Owner_Telnum=jsonfrom.getString("Owner_Telnum");
					
			String sql =  "select * from Tb_Owner where Owner_Telnum="+Owner_Telnum+"";
			try {
				
					stm=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_UPDATABLE);
					rs= stm.executeQuery(sql);
					if(rs.next()){		
						 Manager_Authority=jsonfrom.getInt("Manager_Authority");
						switch(Manager_Authority){
						case 1:{
							double tempdouble=  jsonfrom.getDouble("Balance_Rate");
						   // System.out.println("-----double-------"+tempdouble);
							java.math.BigDecimal tempdecimal= new java.math.BigDecimal(tempdouble);
							 System.out.println("-----decimal-------"+tempdecimal);
						  	rs.updateDouble("Balance_Rate", tempdouble);
							rs.updateString("Balance_Type", jsonfrom.getString("Balance_Type"));
							rs.updateString("Ownner_isAuthority", jsonfrom.getString("Ownner_isAuthority"));	
							rs.updateRow();
							result.put("ActionStatus", true);
							result.put("ErrorInfo", "更新成功");
						           break;}
						case 2:{
							rs.updateString("Owner_Secret", jsonfrom.getString("Owner_Secret"));
							rs.updateString("Parent_Manager_Telnum", jsonfrom.getString("Parent_Manager_Telnum"));
							rs.updateRow();
							result.put("ActionStatus", true);
							result.put("ErrorInfo", "更新成功");
						                break;}
						case 3:{
						       if(rs.getString("Parent_Manager_Telnum").equals(jsonfrom.getString("Manager_Telnum"))){
							rs.updateString("Owner_Name", jsonfrom.getString("Owner_Name"));
							rs.updateString("Owner_Address", jsonfrom.getString("Owner_Address"));	
							rs.updateRow();
							result.put("ActionStatus", true);
							result.put("ErrorInfo", "更新成功");
						         }else{
						        	 result.put("ActionStatus", false);
										result.put("ErrorInfo", "更新失败，不能更改其他区域经理下的业主");
						         }
						        break;}
						// 代表业主自己，可以修改自己的密码
						case 4:{
							rs.updateString("Owner_Secret", jsonfrom.getString("New_Owner_Secret"));
							rs.updateRow();
							result.put("ActionStatus", true);
							result.put("ErrorInfo", "更新成功");
						
						                break;}
						
						}
							
														
										
							
				
					}else{
						result.put("ActionStatus", false);
						result.put("ErrorInfo", "要修改的用户不存在");
					}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				result.put("ActionStatus", false);
				result.put("ErrorInfo", "数据库查询异常");
			}
			
			 break;    } //case2
		// 删除
				case 3:{	
					
					if(Manager_Authority==2){
					 String Owner_Telnum=jsonfrom.getString("Owner_Telnum");
						   try {
							   String sql = "select * from Tb_Owner where Owner_Telnum="+Owner_Telnum+"";
								stm=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
								rs= stm.executeQuery(sql);
								if(rs.next()){
									try{
										
										sql="delete from Tb_Owner where Owner_Telnum="+Owner_Telnum+"";		   
							             stm=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_UPDATABLE);  							           
							            		 stm.executeUpdate(sql);  	
							     				
													result.put("ActionStatus", true);
													result.put("ErrorInfo", "删除成功");
										} catch (SQLException e) {
											// TODO Auto-generated catch block
											e.printStackTrace();
											result.put("ActionStatus", false);
											result.put("ErrorInfo", "数据库删除操作失败");
										} 							
								}else{
									result.put("ActionStatus", false);
									result.put("ErrorInfo", "要删除的用户不存在");
								}
								
							} catch (SQLException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
								result.put("ActionStatus", false);
								result.put("ErrorInfo", "数据库查询操作失败");
							}   
				}else{
					result.put("ActionStatus", false);
					result.put("ErrorInfo", "你没有权限");
				}
						   break;     } //case 3
		// 添加管理员 只有区域经理才有此权限。
		case 4:{
			 if(Manager_Authority==3){
			// step2 检查是否已经存在
			 String Owner_Telnum=jsonfrom.getString("Owner_Telnum");
			   try {
				   String sql = "select * from Tb_Owner where Owner_Telnum="+Owner_Telnum+"";    
					stm=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
					rs= stm.executeQuery(sql);
				if(!rs.next()){	
			// step3 数据库输入。
					try {
						String newStr = new String(jsonfrom.getString("Owner_Name").getBytes(), "utf-8");
						System.out.println("字符转换***********  newString"+newStr);
					} catch (UnsupportedEncodingException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
			
			 String SubManager_Authority="";
			
			sql = "insert  into Tb_Owner (Owner_Telnum,Owner_Secret,Owner_Name,Owner_Address,Parent_Manager_Telnum,Balance_Rate,Balance_Type,Ownner_isAuthority) values (?,?,?,?,?,?,?,?)";
              	pstm=conn.prepareStatement(sql);
				pstm.setString(1, jsonfrom.getString("Owner_Telnum"));
				pstm.setString(2, jsonfrom.getString("Owner_Secret"));
				pstm.setString(3, jsonfrom.getString("Owner_Name"));
				pstm.setString(4, jsonfrom.getString("Owner_Address"));
				pstm.setString(5, jsonfrom.getString("Parent_Manager_Telnum"));
				
			//	java.math.BigDecimal tempdecimal= new java.math.BigDecimal(jsonfrom.getDouble("Balance_Rate"));
				pstm.setDouble(6, jsonfrom.getDouble("Balance_Rate"));
				pstm.setString(7, jsonfrom.getString("Balance_Type"));
				pstm.setString(8, jsonfrom.getString("Ownner_isAuthority"));
				
			    pstm.executeUpdate();
			    result.put("ActionStatus", true);
				result.put("ErrorInfo", "增加成功");
				}else{
					result.put("ActionStatus", false);
					result.put("ErrorInfo", "添加失败，用户已经存在");
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				result.put("ActionStatus", false);
				result.put("ErrorInfo", "数据库异常");
			}
			 }// end of if manager_authority==3
			 else{
				 result.put("ActionStatus", false);
					result.put("ErrorInfo", "你没有权限操作");
			 }
			   
			   break;    } //case4
		
		
		//查询所有
		  case 5:{
			  try {
				 		 
				   String sql = "select * from Tb_Owner";  
					stm=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
					rs= stm.executeQuery(sql);
					JSONArray Owners= new JSONArray();
					while(rs.next()){
						JSONObject owner= new JSONObject();
						boolean isadd=false;
					switch(Manager_Authority){
					
					case 1:{	isadd=true;
					              break;}
					case 2:{	isadd=true;
		                          break;}
					case 3:{
						if(rs.getString("Parent_Manager_Telnum").equals(jsonfrom.getString("Manager_Telnum"))) 
							isadd=true;
						        break;}				
		              }
					if(isadd){
						owner.put("Owner_Telnum", rs.getString("Owner_Telnum"));
                        owner.put("Owner_Name", rs.getString("Owner_Name"));
                        owner.put("Ownner_isAuthority", rs.getString("Ownner_isAuthority"));
                         Owners.add(owner);               
					                         }
					
					}
					result.put("ActionStatus", true);
					result.put("ErrorInfo", "成功");
					result.put("Owners", Owners);
			   
			  }catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					result.put("ActionStatus", false);
					result.put("ErrorInfo", "查询失败");
				} 
					
					
				 break;  } //case5
		
		} //switch
		
               closeConn(conn, pstm, stm, rs);
		
		
		
		return result;
	}
	
	
	
	public static  JSONObject  loginCheck(String jsonData){
		 // step1 所有数据定义
		   JSONObject result= new JSONObject();
	
		   PreparedStatement pstm=null; 
		   Statement  stm=null;
		   ResultSet rs=null; 		 
		   Connection conn= CommonFunction. getConnection();
		 // step2 数据解析 	  
		   JSONObject json= JSONObject.fromObject(jsonData);
		   String Owner_Telnum=json.getString("Owner_Telnum");
		   String Owner_Secret=json.getString("Owner_Secret");
		 
		   try {
		   String sql = "select * from Tb_Owner where Owner_Telnum="+Owner_Telnum+"";  
			stm=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
			rs= stm.executeQuery(sql);
			if(rs.next()){
				String correctSecret= rs.getString("Owner_Secret");
				if(correctSecret.equals(Owner_Secret)){
				String Ownner_isAuthority=rs.getString("Ownner_isAuthority"); 
					if(Ownner_isAuthority.equals("1")){
					result.put("ActionStatus", true);
					result.put("ErrorInfo", "登陆成功");
					}else{
						result.put("ActionStatus", false);
						result.put("ErrorInfo", "该用户没有被授权");
					}
					
				
				}else{
					result.put("ActionStatus", false);
					result.put("ErrorInfo", "密码不正确");
				}
			}else{
				result.put("ActionStatus", false);
				result.put("ErrorInfo", "不存在该用户");
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		   
		   closeConn(conn, pstm, stm, rs);
		   return result;
	 }
	
	public static JSONObject authorityCheck(String Owner_Telnum,Connection your_conn){
		 JSONObject result= new JSONObject();
		 PreparedStatement pstm=null; 
		   Statement  stm=null;
		   ResultSet rs=null; 	
		   try {
			   String sql = "select * from Tb_Owner where Owner_Telnum="+Owner_Telnum+"";  
				stm=your_conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
				rs= stm.executeQuery(sql);
				if(rs.next()){
					String Ownner_isAuthority=rs.getString("Ownner_isAuthority"); 
					if(Ownner_isAuthority.equals("1")){
						 result.put("ActionStatus", true);
					     result.put("ErrorInfo", "所属业主授权检查成功");
					}else{
						 result.put("ActionStatus", false);
					     result.put("ErrorInfo", "所属业主授权检查失败，,业主未授权,");
					}
					
				}else{
					 result.put("ActionStatus", false);
						result.put("ErrorInfo", "所属业主授权检查失败，,业主不存在,");
				}
			   
		   }catch(Exception e){
			   e.printStackTrace();
			   result.put("ActionStatus", false);
				result.put("ErrorInfo", "所属业主授权检查时，数据库异常");
		   } 
		CommonFunction.closeConn(null, pstm, stm, rs);
		return result;
	}
	
	
	
	 private static void closeConn(Connection conn,PreparedStatement pstm,Statement stm, ResultSet rs){
		 
		 
			try {
				 if(conn!=null) conn.close();
				 if(pstm!=null) pstm.close();
				 if(stm!=null) stm.close();
				 if(rs!=null) rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		 
	 }
}
